const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
const { ProvidePlugin } = require('webpack');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

// css压缩
const CssMinimizerWebpackPlugin = require('css-minimizer-webpack-plugin');

module.exports = {
  entry: './src/index.js',
  devtool: false, //'source-map',
  mode: 'development',
  output: {
    filename: 'js/[name]_bundle.js',
    path: path.resolve(__dirname, './build'),
    clean: true
    // chunkFilename: '[name]_chunk.js'
  },
  externals: {
    jquery: 'jQuery'
  },
  // 优化配置
  optimization: {
    // 生成的chunkid算法
    // named  development 推荐
    // 默认的 deterministic production 推荐
    chunkIds: 'deterministic',
    // runtimeChunk: {
    //   // 脚手架不配置了
    //   name: 'runtime'
    // },
    splitChunks: {
      // 分包
      chunks: 'all',
      // maxSize: 20 * 1024,
      // minSize: 10 * 1024,
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          filename: 'js/[id]_verdors.js'
        },
        routers: {
          test: /router/,
          filename: 'js/[id]router_verdors.js'
        }
      }
    },
    // 开启 develop压缩
    minimize: true,
    minimizer: [
      new TerserPlugin({ extractComments: false, parallel: 4 }),
      new CssMinimizerWebpackPlugin({ parallel: 4 })
    ]
  },

  devServer: {
    // hot:true,
    static: ['public', 'vender'],
    compress: true,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:9000',
        pathRewrite: { '^/api': '' }
        // changeOrigin: true
      },
      port: 3000
    },
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.(m|e)?jsx?$/,
        use: [
          {
            loader: 'babel-loader'
          }
        ]
      },
      {
        test: /\.ts$/i,
        //use:'ts-loader'
        use: ['babel-loader']
      },
      {
        test: /\.css$/,
        use: [MiniCssExtractPlugin.loader, 'css-loader']
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './index.html'
    }),
    new ProvidePlugin({
      axios: ['axios', 'default'],
      get: ['axios', 'default', 'get']
      // 'dayjs':'dayjs',
      // timeFormort:['dayjs','format']
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[name]_[hash:6].css',
      chunkFilename: 'css/[name]_chunk.css'
    })
  ]
};
